package com.pengzy.comment.web.webFluxUtils;

import com.pengzy.comment.core.utils.JsonUtils;
import com.pengzy.comment.web.exception.CustomException;
import lombok.SneakyThrows;
import reactor.core.publisher.Mono;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author xiaozhi 2023/8/3
 */

public class webFluxUtil<T> {





    /**
     * 非阻塞方式获取,推荐
     * @param mono
     * @param <T>
     * @return
     */
    public static  <T>T getMonoValue(Object mono){
        if(mono==null){
            return null;
        }
        AtomicReference<T> value = new AtomicReference<>(null);
        if(mono instanceof Mono) {
            ((Mono) mono)
                    .doOnNext(s -> {
                        value.set((T) s);
                    }).subscribe();
        }else{
            throw new CustomException("非Mono类...");
        }
        return value.get();
    }

    @SneakyThrows
    public static  <T>T getMonoValueFauth(Mono<T> mono) {
        if(mono==null){
            return null;
        }
        CompletableFuture<T> block = mono.toFuture();
        return block.get();
    }



    /**
     * 阻塞式方式获取
     * @param mono
     * @param <T>
     * @return
     */
    public static <T>T getMonoValueBlock(Mono<T> mono){
        if(mono==null){
            return null;
        }
        return mono.block();
    }
}
